package com.by.component.tencent.cos;

import com.google.common.collect.Lists;
import com.by.toolkit.utils.SplitterUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboService;

import javax.annotation.Resource;
import java.io.File;
import java.util.Map;

import static com.by.toolkit.utils.ExceptionUtil.nullError;

@Slf4j
@DubboService
public class CosServiceImpl implements CosService {

    @Resource
    private StaticResourcesMapper staticResourcesMapper;

    @Resource
    private StaticResourcesService staticResourcesService;

    @Resource
    private TencentCosSupport tencentCosSupport;

    @Override
    public String uploadFile2Url(Integer resourceType, String key, byte[] bytes) {
        ResourcesTypeBasicSDTO basic = byListUtil.getFirstObj(staticResourcesMapper.resourcesTypeBasic(resourceType, StaticResourcesPlatformTypeEnum.TENCENT.getCode()));
        nullError(basic, "资源类型[{}]对应的基础信息不存在");

        tencentCosSupport.putObject(basic.getBucketName(), key, bytes);
        return SplitterUtil.EN_OBLIQUE_LINE_JOINER.join(basic.getDomainName(), key);
    }

    @Override
    public Long uploadFile2Id(Integer resourceType, String key, byte[] bytes) {
        AcquireResourceRespSDTO acquireResource = staticResourcesService.acquireResource(AcquireResourceSDTO.builder()
                .platformType(StaticResourcesPlatformTypeEnum.TENCENT.getCode())
                .items(Lists.newArrayList(AcquireResourceItemSDTO.builder()
                        .resourceType(resourceType)
                        .resourceKey(key)
                        .build()))
                .build());

        Map<String, Long> resourceIdMap = acquireResource.getResourceIdMap();
        Map<Integer, String> bucketNameMap = acquireResource.getBucketNameMap();

        tencentCosSupport.putObject(bucketNameMap.get(resourceType), key, bytes);
        return resourceIdMap.get(key);
    }

    @Override
    public Long uploadFile2Id(Integer resourceType, String key, File file) {
        AcquireResourceRespSDTO acquireResource = staticResourcesService.acquireResource(AcquireResourceSDTO.builder()
                .platformType(StaticResourcesPlatformTypeEnum.TENCENT.getCode())
                .items(Lists.newArrayList(AcquireResourceItemSDTO.builder()
                        .resourceType(resourceType)
                        .resourceKey(key)
                        .build()))
                .build());

        Map<String, Long> resourceIdMap = acquireResource.getResourceIdMap();
        Map<Integer, String> bucketNameMap = acquireResource.getBucketNameMap();

        tencentCosSupport.putObject(bucketNameMap.get(resourceType), key, file);
        return resourceIdMap.get(key);
    }

    @Override
    public Long uploadFile2Id(Integer resourceType, String key, String content) {
        AcquireResourceRespSDTO acquireResource = staticResourcesService.acquireResource(AcquireResourceSDTO.builder()
                .platformType(StaticResourcesPlatformTypeEnum.TENCENT.getCode())
                .items(Lists.newArrayList(AcquireResourceItemSDTO.builder()
                        .resourceType(resourceType)
                        .resourceKey(key)
                        .build()))
                .build());

        Map<String, Long> resourceIdMap = acquireResource.getResourceIdMap();
        Map<Integer, String> bucketNameMap = acquireResource.getBucketNameMap();

        tencentCosSupport.putObject(bucketNameMap.get(resourceType), key, content);
        return resourceIdMap.get(key);

    }

    @Override
    public void copyFile(String sourceBucketName, String sourceKey, String targetBucketName, String targetKey) {
        tencentCosSupport.copyObject(sourceBucketName, sourceKey, targetBucketName, targetKey);
    }

}
